<?php

namespace app\api\controller;

use app\api\server\Order;
use app\api\server\Refund as ServerRefund;
use app\common\controller\Api;
use app\common\model\order\Goods;
use app\common\model\order\Order as OrderOrder;
use app\common\model\order\Refund as OrderRefund;
use app\common\model\order\Refundconfig as OrderRefundconfig;

/**
 * 售后接口
 */
class Refund extends Api
{
    protected $noNeedLogin = ['apply_after', 'refund_lists'];
    protected $noNeedRight = ['*'];
    /**
     * 退款原因配置
     *
     */
    public function config()
    {
        $list = OrderRefundconfig::column('name');
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @date: 2023-11-16 
     * @ApiTitle (申请商品售后)
     * @ApiMethod (GET)
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="o_g_id", type="string", required=true, sample="", description="订单商品ID")
     * @ApiParams (name="refund_type", type="string", required=true, sample="", description="退款类型:1=仅退款,2=退货退款")
     * @ApiParams (name="refund_reason", type="string", required=true, sample="", description="退款原因")
     * @ApiParams (name="refund_mark", type="string", required=true, sample="", description="退款备注")
     * @ApiParams (name="image", type="string", required=true, sample="", description="凭证")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function apply_after()
    {
        $params = request()->param();
        try {
            $params['user_id'] = $this->auth->id;
            ServerRefund::apply_refund($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }

    /**
     * @Author LK
     * @date: 2023-12-26 
     * @ApiTitle (可售后列表)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function after_lists()
    {
        $aftertime = strtotime("+" . config('site.auto_settlement'));
        $order_ids = OrderOrder::whereTime('paytime', '<', $aftertime)->where('type', '1')->where('user_id', $this->auth->id)->where('refund_status', '<>', '20')->column('id');
        $lists = Goods::with(['spec'])->whereIn('order_id', $order_ids)->whereIn('refund_status', ['0', '-1'])->order('id desc')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2023-12-26 
     * @ApiTitle (可售后详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function after_lists_info()
    {
        $params = request()->param();
        extract($params);
        $info = Goods::get($id, ['spec']);
        $this->success('', $info);
    }
    /**
     * @Author LK
     * @date: 2023-12-26 
     * @ApiTitle (已申请售后列表)
     * @ApiMethod (GET)
     * @ApiParams (name="refund_status", type="integer", required=true, sample="", description="10=售后中,20=售后成功")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function already_lists()
    {
        $params = request()->param();
        extract($params);
        if (!isset($refund_status)) {
            $this->error('参数错误');
        }
        if ($refund_status == '10') {
            $where['refund_status'] = ['in', ['0', '10']];
        }
        if ($refund_status == '20') {
            $where['refund_status'] = ['in', ['20', '-1']];
        }
        $lists = OrderRefund::with(['goods','spec'])->where($where)->where('user_id', $this->auth->id)->order('id desc')->paginate();
        $this->success('', $lists);
    }

    /**
     * @Author LK
     * @date: 2023-12-26 
     * @ApiTitle (售后详情)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function after_info()
    {
        $id = request()->param('id', 0);
        $this->success('', OrderRefund::get($id, ['goods','spec']));
    }
    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (撤销售后)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function cancel_after()
    {
        $params = request()->param();
        try {
            ServerRefund::cancel_after($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success();
    }

    /**
     * @Author LK
     * @date: 2023-11-28 
     * @ApiTitle (填写快递)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="express_com", type="integer", required=true, sample="", description="快递名称")
     * @ApiParams (name="express_no", type="integer", required=true, sample="", description="快递单号")
     * @ApiParams (name="express_mark", type="integer", required=true, sample="", description="快递说明")
     * @ApiParams (name="express_images", type="integer", required=true, sample="", description="快递凭证")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function add_express()
    {
        $params = request()->param();
        try {
            ServerRefund::add_express($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success();
    }
}
